package com.lottery.game.core.modules.service.game.impl;

import com.lottery.game.common.base.service.impl.AbstractBaseServiceImpl;
import com.lottery.game.common.bean.BeanConverter;
import com.lottery.game.core.model.dto.game.CurrentGameIssue;
import com.lottery.game.core.model.dto.game.GameTypePlayingLotteryRuleTO;
import com.lottery.game.core.model.dto.game.GameTypePlayingRelationTO;
import com.lottery.game.core.model.dto.game.GameTypeTO;
import com.lottery.game.core.modules.entity.game.GameGame;
import com.lottery.game.core.modules.entity.game.GameType;
import com.lottery.game.core.modules.entity.game.GameTypePlayingRelation;
import com.lottery.game.core.modules.entity.game.SysDict;
import com.lottery.game.core.modules.mapper.v2.GameTypePlayingRelationMapper;
import com.lottery.game.core.modules.service.game.GameGameService;
import com.lottery.game.core.modules.service.game.GameTypePlayingLotteryRuleService;
import com.lottery.game.core.modules.service.game.GameTypePlayingRelationService;
import com.lottery.game.core.modules.service.game.GameTypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.lottery.game.common.utils.DateUtils.isCurrentTimeIn;

@Service("gameTypePlayingRelationService")
public class GameTypePlayingRelationServiceImpl extends AbstractBaseServiceImpl<GameTypePlayingRelation> implements GameTypePlayingRelationService {

    @Autowired
    private GameTypeService gameTypeService;

    @Autowired
    private GameTypePlayingLotteryRuleService gameTypePlayingLotteryRuleService;

    @Autowired
    private GameGameService gameGameService;

    @Autowired
    private GameTypePlayingRelationMapper gameTypePlayingRelationMapper;

    @Override
    public GameTypePlayingRelationTO find(String id) {
        GameTypePlayingRelationTO relationTO = selectByPk(id).convert(GameTypePlayingRelationTO.class);
        setRule(relationTO);
        return relationTO;
    }

    @Override
    public GameTypePlayingRelationTO findByCode(String code) {
        GameTypePlayingRelation param = new GameTypePlayingRelation();
        param.setDelFlag("0");
        param.setGameTypePlayingCode(code);
        GameTypePlayingRelationTO relationTO = selectOne(param).convert(GameTypePlayingRelationTO.class);
        setRule(relationTO);
        return relationTO;
    }

    @Override
    public List<GameTypeTO> playings() {
        GameType param = new GameType();
        param.setDelFlag("0");
        List<GameType> gameTypes = gameTypeService.select(param);
        List<GameTypeTO> gameTypeTOS = BeanConverter.convert(GameTypeTO.class, gameTypes);
        for (GameTypeTO gameTypeTO : gameTypeTOS) {
            GameTypePlayingRelation parameters = new GameTypePlayingRelation();
            parameters.setGameTypeId(gameTypeTO.getId());
            parameters.setDelFlag("0");
            List<GameTypePlayingRelation> relations = this.select(parameters);
            gameTypeTO.setPlayings(BeanConverter.convert(GameTypePlayingRelationTO.class, relations));
            for (GameTypePlayingRelationTO relationTO : gameTypeTO.getPlayings()) {
                this.setRule(relationTO);
            }
        }
        return gameTypeTOS;
    }

    @Override
    public List<GameTypePlayingRelationTO> all() {
        GameTypePlayingRelation parameters = new GameTypePlayingRelation();
        parameters.setDelFlag("0");
        List<GameTypePlayingRelationTO> relationTOS = BeanConverter.convert(GameTypePlayingRelationTO.class, this.select(parameters));
        for (GameTypePlayingRelationTO relationTO : relationTOS) {
            this.setRule(relationTO);
        }

        return relationTOS;
    }

    @Override
    public GameTypePlayingRelationTO findByTicket(Integer ticket) {
        GameTypePlayingRelation param = new GameTypePlayingRelation();
        param.setDelFlag("0");
        param.setGameTypePlayingTicket(ticket);
        GameTypePlayingRelation relation = selectOne(param);
        GameTypePlayingRelationTO relationTO = null;

        if(relation != null) {
        	relationTO = relation.convert(GameTypePlayingRelationTO.class);
        	setRule(relationTO);
        }
        return relationTO;
    }

    @Override
    public GameTypePlayingRelationTO findByTicketAndRoomid(Integer ticket, String roomid) {
        GameTypePlayingRelation param = new GameTypePlayingRelation();
        param.setDelFlag("0");
        param.setGameTypePlayingTicket(ticket);
        GameTypePlayingRelation relation = selectOne(param);
        GameTypePlayingRelationTO relationTO = null;

        if(relation != null) {
            relationTO = relation.convert(GameTypePlayingRelationTO.class);
            setRule(relationTO, roomid);
        }
        return relationTO;
    }

    @Override
    public List<GameTypePlayingRelationTO> findByGameTypeCode(String typeCode) {
        GameType param = new GameType();
        param.setDelFlag("0");
        param.setCode(typeCode);
        GameType gameType = gameTypeService.selectOne(param);
        if (gameType == null) {
            return null;
        }
        GameTypePlayingRelation parameters = new GameTypePlayingRelation();
        parameters.setGameTypeId(gameType.getId());
        parameters.setDelFlag("0");
        List<GameTypePlayingRelation> relations = this.select(parameters);
        List<GameTypePlayingRelationTO> relationTOS = BeanConverter.convert(GameTypePlayingRelationTO.class, relations);
        for (GameTypePlayingRelationTO relationTO : relationTOS) {
            this.setRule(relationTO);
        }
        return relationTOS;
    }

    @Override
    public void updateGameTypeOpenOrCloseByticket(int ticket, int open) {
        gameTypePlayingRelationMapper.updateGameTypeOpenOrCloseByticket(ticket,open);
    }

    @Override
    public List<SysDict> getGroup() {
        return gameTypePlayingRelationMapper.getGroup();
    }

    private void setRule(GameTypePlayingRelationTO relationTO) {
        List<GameTypePlayingLotteryRuleTO> ruleTOS = gameTypePlayingLotteryRuleService.rules(relationTO.getId());
        if (relationTO.getGameTypePlayingTicket() != null) {
            GameGame gameGame = gameGameService.findLastGameIssueByTicketId(relationTO.getGameTypePlayingTicket());
            if (gameGame != null) {
                relationTO.setCurrentIssue(gameGame.convert(CurrentGameIssue.class));
            }
        }
        for (GameTypePlayingLotteryRuleTO rule : ruleTOS) {
            if (isCurrentTimeIn(rule.getRuleStartTime(), rule.getRuleEndTime())) {
                relationTO.setDurationTime(rule.getDurationTime());
                relationTO.setClosedTime(rule.getClosedTime());
                return;
            }
        }

    }

    private void setRule(GameTypePlayingRelationTO relationTO, String roodmin) {
        List<GameTypePlayingLotteryRuleTO> ruleTOS = gameTypePlayingLotteryRuleService.rules(relationTO.getId());
        if (relationTO.getGameTypePlayingTicket() != null) {
            GameGame gameGame = gameGameService.findLastGameIssueByIsNotNullParam(GameGame.builder().
                    roomid(roodmin)
                    .gameTypePlayingTicket(relationTO.getGameTypePlayingTicket())
                    .build());
            if (gameGame != null) {
                relationTO.setCurrentIssue(gameGame.convert(CurrentGameIssue.class));
            }
        }
        for (GameTypePlayingLotteryRuleTO rule : ruleTOS) {
            if (isCurrentTimeIn(rule.getRuleStartTime(), rule.getRuleEndTime())) {
                relationTO.setDurationTime(rule.getDurationTime());
                relationTO.setClosedTime(rule.getClosedTime());
                return;
            }
        }

    }
}
